﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;

namespace HoverTree.Hewenqi
{
    public partial class ListViewForm : Form
    {
        public ListViewForm()
        {
            InitializeComponent();
        }

        FileInfo[] _files=null;
        DirectoryInfo _TheFolder;

        private void button_ok_Click(object sender, EventArgs e)
        {
            string m_selectPath;
            if (folderBrowserDialog_hvt.ShowDialog() == DialogResult.OK)
            {
                m_selectPath = folderBrowserDialog_hvt.SelectedPath;

                 _TheFolder = new DirectoryInfo(m_selectPath);

                BindContent();
            }
                
        }

        void BindContent()
        {

                listView_hoverTree.Clear();



                
                listView_hoverTree.BeginUpdate(); //数据更新，UI暂时挂起，直到EndUpdate绘制控件，可以有效避免闪烁并大大提高加载速度

                //listView_hoverTree.View = View.Details;//设置视图  
                //listView_hoverTree.SmallImageList = imageList;//设置图标 

                //添加列  
                listView_hoverTree.Columns.Add("名称", 300, HorizontalAlignment.Left);
                listView_hoverTree.Columns.Add("路径", 300, HorizontalAlignment.Left);
                // ListViewItem m_item;
                foreach (DirectoryInfo di in _TheFolder.GetDirectories())//遍历文件夹下所有文件夹
                {
                    var m_item = new ListViewItem();
                    m_item.ImageIndex = 0; //通过与imageList绑定，显示imageList中第i项图标
                    m_item.Text = di.Name;
                    m_item.SubItems.Add(di.FullName); //路径 何问起  
                    //添加行  
                    listView_hoverTree.Items.Add(m_item);
                }

                _files = _TheFolder.GetFiles();
                foreach (FileInfo fi in _files)//遍历文件夹下所有文件
                {
                    var m_item = new ListViewItem();
                    m_item.ImageIndex = 1;
                    m_item.Text = fi.Name;
                    m_item.SubItems.Add(fi.FullName);
                    //添加行  
                    listView_hoverTree.Items.Add(m_item);
                }
                listView_hoverTree.EndUpdate();//结束数据处理，UI界面一次性绘制。 

        }

        private void button_rename_Click(object sender, EventArgs e)
        {
            if(!(MessageBox.Show("是否重命名文件名？","确认",  MessageBoxButtons.OKCancel)== DialogResult.OK))
            {
                return;
            }
            List<string> h_suffixList = new List<string>();
            Dictionary<string, int> h_suffixIndex = new Dictionary<string, int>();

            if(_files!=null)
            {
                foreach(FileInfo fi in _files)
                {
                  if(h_suffixList.Contains(fi.Extension)) {
                        h_suffixIndex[fi.Extension] = h_suffixIndex[fi.Extension] + 1;
                    }
                  else
                    {
                        h_suffixList.Add(fi.Extension);
                        h_suffixIndex.Add(fi.Extension,1);
                    }
                    string h_newFielName = fi.DirectoryName +"\\"+textBox_prefix.Text.Trim()+ h_suffixIndex[fi.Extension].ToString() + fi.Extension;

                    fi.MoveTo(h_newFielName);
               }
                BindContent();
            }

        }
    }
}